/**
 * @Author Mrlu
 * @Version 1.0
 * @Date 2023-08-02 16:37:36
 */
package com.cl.erp_2301.controller;


import com.cl.erp_2301.Service.OrdersService;
import com.cl.erp_2301.dto.*;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v2")
public class OrdersController {

    @Resource
    private OrdersService ordersService;


    /*所有数据*/
    @GetMapping("/market/listByPage")
    public Result listByPage(Integer state, int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<OrdersDTO> ordersList = ordersService.listByPage(state);
        DataDTO dataDTO = new DataDTO();
        for (OrdersDTO ordersDTO : ordersList) {
            List<OrdersDetailDTO> ordersdetailDTOList = ordersService.selectOrderDetailByUuId(ordersDTO.getUuid());
            ordersDTO.setOrderDetailList(ordersdetailDTOList);
        }
        dataDTO.setRows(ordersList);
        dataDTO.setTotal(page1.getTotal());
        return Result.ok(dataDTO);
    }

/*    *//*点击新增数据*//*
    @GetMapping("/orders/listByPage1")
    public Result listByPage1() {
        List<OrdersDetailsDTO> ordersList = ordersService.listByPage1();
        return Result.okRows(ordersList);
    }*/


    @GetMapping("/market/showsupplier")
    public Result showsupplier(int type) {
        List<OrdersDTO> ordersDtoList = ordersService.showsupplier(type);
        DataDTO dataDTO = new DataDTO<>();
        dataDTO.setRows(ordersDtoList);
        return Result.ok(dataDTO);
    }

    /*添加销售订单*/
    @PostMapping("/market/add")
    public Result orderAdd(@RequestBody OrdersAddDTO ordersAddDTO, HttpServletRequest request, HttpServletResponse response) {
        Date date = new Date();
        Integer customeruuid = ordersAddDTO.getCustomeruuid();
        for (MarketDTO marketDetails : ordersAddDTO.getMarketDetails()) {
            marketDetails.setEndtime(date);
            marketDetails.setState(0);
            marketDetails.setStoreuuid(customeruuid);
        }
        ordersService.orderAdd(ordersAddDTO.getMarketDetails());
        return Result.ok();


    }

    /*出库显示数据*/
    @GetMapping("/returnorders/selectStore")
    public Result selectStore() {
        List<OrdersDTO> ordersDetailDTOS = ordersService.selectStore();
        DataDTO dataDTO = new DataDTO<>();
        dataDTO.setRows(ordersDetailDTOS);
        return Result.ok(dataDTO);
    }

    /*出库*/
    @PostMapping("/marketdetail/doOutStore")
    public Result doOut(HttpSession session, OrdersDetailDTO ordersDetailDTO) {
        Integer id = (Integer) session.getAttribute("goodsuuid");
        ordersDetailDTO.setUuid(id);
        ordersService.doOut(ordersDetailDTO);
        return Result.ok();
    }
}
